package com.jacob.cloud.mall.categoryproduct.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jacob.cloud.mall.categoryproduct.model.dao.CategoryMapper;
import com.jacob.cloud.mall.categoryproduct.model.pojo.Category;
import com.jacob.cloud.mall.categoryproduct.model.request.AddCategoryReq;
import com.jacob.cloud.mall.categoryproduct.model.request.UpdateCategoryReq;
import com.jacob.cloud.mall.categoryproduct.model.vo.CategoryVO;
import com.jacob.cloud.mall.categoryproduct.service.ICategoryService;
import com.jacob.cloud.mall.common.exception.MallException;
import com.jacob.cloud.mall.common.exception.MallExceptionEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 添加分类
     * @param addCategoryReq 新增分类请求数据
     */
    @Override
    public void add(AddCategoryReq addCategoryReq){
        Category category = new Category();
        BeanUtils.copyProperties(addCategoryReq, category);

        // 校验分类名称是否重复
        Category categoryOld = categoryMapper.selectByName(category.getName());
        if (!(categoryOld == null)){
            throw new MallException(MallExceptionEnum.CATEGORY_NAME_EXIST);
        }
        int count = categoryMapper.insertSelective(category);
        if (count == 0){
            throw new MallException(MallExceptionEnum.INSERT_FAIL);
        }
    }

    /**
     * 修改分类
     * @param updateCategoryReq 修改分类请求数据
     */
    @Override
    public void update(UpdateCategoryReq updateCategoryReq) {
        Category category = new Category();
        BeanUtils.copyProperties(updateCategoryReq, category);

        // 修改分类的名称不能与数据库中已有的分类名相同
        Category categoryOld = categoryMapper.selectByName(category.getName());
        if (!(categoryOld == null) && !category.getId().equals(categoryOld.getId())) {
            throw new MallException(MallExceptionEnum.CATEGORY_NAME_EXIST);
        }
        int count = categoryMapper.updateByPrimaryKeySelective(category);
        if (count == 0) {
            throw new MallException(MallExceptionEnum.UPDATE_FAIL);
        }
    }

    /**
     * 删除分类
     * @param categoryId 分类id
     */
    @Override
    public void delete(Integer categoryId) {
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if (category == null) {
            throw new MallException(MallExceptionEnum.CATEGORY_NOT_EXIST);
        }
        int count = categoryMapper.deleteByPrimaryKey(categoryId);
        if (count == 0) {
            throw new MallException(MallExceptionEnum.DELETE_FAIL);
        }
    }

    /**
     * 分页查询分类列表
     * @param pageNum 页码
     * @param pageSize 页大小
     */
    @Override
    public PageInfo<Category> listCategory(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, "type,order_num");
        List<Category> categoryList = categoryMapper.selectList();
        return new PageInfo<Category>(categoryList);
    }

    /**
     * 分类列表
     */
    @Override
    @Cacheable(value = "listCategoryForCustomer")
    public List<CategoryVO> listCategoryForCustomer(Integer parentId) {
        ArrayList<CategoryVO> categoryVOList = new ArrayList<CategoryVO>();
        recursivelyFindCategories(categoryVOList, parentId);
        return categoryVOList;
    }

    /**
     * 递归查询商品分级目录
     * @param categoryVOList  父分类id相同的分类列表
     * @param parentId 父分类id
     */
    public void recursivelyFindCategories(List<CategoryVO> categoryVOList, Integer parentId) {
        List<Category> categoryList = categoryMapper.selectByParentId(parentId);
        if (!CollectionUtils.isEmpty(categoryList)) {
            for (Category category : categoryList) {
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category, categoryVO);
                categoryVOList.add(categoryVO);
                // 递归获取子目录
                recursivelyFindCategories(categoryVO.getChildCategory(), categoryVO.getId());
            }
        }
    }

}
